﻿#include "wswitchnode.h"
#include "wnodemanager.h"
#include "wxpolygonex.h"
#include "../common.h"
#include "../scriptmanager.h"
#include <wx/strconv.h>

WSwitchNode::WSwitchNode()
    : m_SwitchState(SwitchState::POSITIVE)
{
    this->setNodeType(NODE_SWITCH);
}

WSwitchNode::WSwitchNode(const wxString& pname)
    : WBaseNode(pname),
      m_SwitchState(SwitchState::POSITIVE)
{
    this->setNodeType(NODE_SWITCH);
}

WSwitchNode::~WSwitchNode()
{

}

/// 建立脚本文件
bool WSwitchNode::createScriptFile(void)
{
    // 目标文件
    /*const wxString decScriptFile = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath()+
                             wxString::Format("\\resources\\scripts\\nodes\\%s.lua",this->getName());

    if(wxFileExists(decScriptFile))
    {
        /// 注册结点到lua中
        ScriptManager::getSingleton().registerBaseNodetoLua("S_",this);

        return false;
    }

    const wxString decScriptDir = wxFileName(wxStandardPaths::Get().GetExecutablePath()).GetPath()+
            "\\resources\\scripts\\nodes";

    // 检测指定目录是否存在，不存在创建目录
    if(!wxDir::Exists(decScriptDir))
    {
        if (!wxFileName::Mkdir(decScriptDir, 0755, wxPATH_MKDIR_FULL))
            return false;
    }

    wxString tmpScriptContent = wxString("--结点更新\n"
                                         "function update_%s()\n"
                                         "\n"
                                         "end\n"
                                         "\n"
                                         "--[[鼠标事件处理]]--\n"
                                         "function onMouseEvent_%s(event)\n"
                                         "\n"
                                         "end\n"
                                         "\n"
                                         "--绘制结点\n"
                                         "function draw_%s(painter)\n"
                                         "   local p%s = WSwitchNode.castFromBase(%s);\n"
                                         "\n"
                                         "end");
    tmpScriptContent.Replace("%s","S_"+this->getName());


    wxFile decfile;
    if (!decfile.Create(decScriptFile, true))
        return false;

    wxCSConv conv("GB2312");

    if (!decfile.Write(tmpScriptContent,conv)) {
        decfile.Close();
        return false;
    }

    decfile.Close();*/

    /// 注册结点到lua中
    ScriptManager::getSingleton().registerBaseNodetoLua("S_",this);

    return true;
}

/// 设置结点管理器
void WSwitchNode::setNodeManager(WNodeManager *pNodeManager)
{
    WBaseNode::setNodeManager(pNodeManager);

    // 挂接道岔改变信号槽
    sig_switch_change.connect(pNodeManager,&WNodeManager::slot_processSwitchChangeToSect);
}

/// 设置道岔状态
void WSwitchNode::setSwitchState(int state)
{
    bool isSendSignal = false;

    if(m_SwitchState != (SwitchState)state)
        isSendSignal = true;

    m_SwitchState = (SwitchState)state;

    if(isSendSignal) sig_switch_change.emitt();
}

/// 初始道岔状态
void WSwitchNode::initSwitchState(int state,wxString startpos,wxString endpos)
{
    if(startpos == "" || endpos == "")
        return;

    tagMountPoint* pstartpos = this->getMountPointByName(startpos);
    tagMountPoint* pendpos = this->getMountPointByName(endpos);

    // 挂接点找不到或者方向一致都不行
    if((pstartpos == NULL || pendpos == NULL) ||
       pstartpos->direction == pendpos->direction)
        return;

    m_SwitchStateMounts[(SwitchState)state] = SwitchStateMounts(pstartpos,pendpos);
}

/// 工作区域改变
void WSwitchNode::changeWorkingRect(wxRect workingrect)
{
    WBaseNode::changeWorkingRect(workingrect);

    // 计算实际显示点
    mScreenCenterPosition = wxRealPoint(workingrect.GetLeftTop().x+workingrect.GetWidth()*(mCenterPosition.x/100.0f),
                              workingrect.GetLeftTop().y+workingrect.GetHeight()*(mCenterPosition.y/100.0f));

    int tmpSize = IDD_SWITCHRECT_SIZE/2;

    // 计算包围盒
    wxPoint points[4];
    points[0] = wxPoint(mScreenCenterPosition.x-tmpSize,mScreenCenterPosition.y-tmpSize);
    points[1] = wxPoint(mScreenCenterPosition.x+tmpSize,mScreenCenterPosition.y-tmpSize);
    points[2] = wxPoint(mScreenCenterPosition.x+tmpSize,mScreenCenterPosition.y+tmpSize);
    points[3] = wxPoint(mScreenCenterPosition.x-tmpSize,mScreenCenterPosition.y+tmpSize);

    this->setBoundingRect(wxPolygonEx(4,points));
}

/// 更新
void WSwitchNode::update(void)
{
    // 调用lua更新脚本接口
    ScriptManager::getSingleton().callLuaFunctionWithBaseNodeUpdate("S_"+this->getName());
}

/// 处理鼠标事件
bool WSwitchNode::OnMMouseEvent(wxMouseEvent& event)
{
    /// 调用结点的鼠标事件接口
    ScriptManager::getSingleton().callLuaFunctionWithBaseNodeMouseEvent("S_"+this->getName(),
                                                                        event);

    return false;
}
